using System;
using System.Collections.Generic;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.ComponentModel;

namespace BreakReminder
{
    public static class IsolatedStoreDataSource
    {
        static IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Roaming, null, null);
        const String FileName = "BreakReminder.txt";
        static readonly String[] INIT_SCHEDULED_TIME = new String[] { "11:00", "15:00" };
        const String INIT_INTERVAL = "1.5";
        const String INTERVAL_KEY = "Interval";
        const String SCHEDULED_TIME_KEY = "ScheduledTimes";
        const String USE_INTERVAL_KEY = "UseInterval";
        const String IS_PLAY_SOUND = "IsPlaySound";
        const String IS_INTERNAL_SOUND = "IsInternalSound";
        const String SOUND_FILE = "SoundFile";


        public static String[] ScheduledTimes
        {
            get
            {
                String val = Read(SCHEDULED_TIME_KEY);
                return val == null ? INIT_SCHEDULED_TIME : val.Split(new char[] { ',' });
            }
            set
            {
                Write(Interval, value, UseInterval, IsPlaySound, IsInternalSound, SoundFile);
            }
        }

        public static String Interval
        {
            get
            {
                String val = Read(INTERVAL_KEY);
                return val ?? INIT_INTERVAL;
            }

            set
            {
                Write(value, ScheduledTimes, UseInterval, IsPlaySound, IsInternalSound, SoundFile);
            }
        }

        public static bool UseInterval
        {
            get
            {
                String val = Read(USE_INTERVAL_KEY);
                return Boolean.Parse(val ?? Boolean.TrueString);
            }
            set
            {
                Write(Interval, ScheduledTimes, value, IsPlaySound, IsInternalSound, SoundFile);
            }
        }

        public static bool IsPlaySound
        {
            get
            {
                String val = Read(IS_PLAY_SOUND);
                return Boolean.Parse(val ?? Boolean.FalseString);
            }
            set
            {
                Write(Interval, ScheduledTimes, UseInterval, value, IsInternalSound, SoundFile);
            }
        }

        public static bool IsInternalSound
        {
            get
            {
                String val = Read(IS_INTERNAL_SOUND);
                return Boolean.Parse(val ?? Boolean.FalseString);
            }
            set
            {
                Write(Interval, ScheduledTimes, UseInterval, IsPlaySound, value, SoundFile);
            }
        }

        public static String SoundFile
        {
            get
            {
                return Read(SOUND_FILE) ?? "";
            }
            set
            {
                Write(Interval, ScheduledTimes, UseInterval, IsPlaySound, IsInternalSound, value);
            }
        }

        public static String Read(String key)
        {
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read, isoStore));
            String s;
            while ((s = reader.ReadLine()) != null && !s.StartsWith(key + "=")) ;
            reader.Close();
            if (String.IsNullOrEmpty(s)) return null;
            return s.Substring((key + "=").Length);
        }

        public static void Write(String _interval, String[] _scheduledTimes, bool _useInterval, bool _isPlaySound, bool _isInternalSound, String _soundFile)
        {
            StreamWriter writer = null;
            writer = new StreamWriter(new IsolatedStorageFileStream(FileName, FileMode.Create, isoStore));
            writer.WriteLine(INTERVAL_KEY + "=" + _interval);
            writer.WriteLine(SCHEDULED_TIME_KEY + "=" + String.Join(",", _scheduledTimes));
            writer.WriteLine(USE_INTERVAL_KEY + "=" + _useInterval.ToString());
            writer.WriteLine(IS_PLAY_SOUND + "=" + _isPlaySound.ToString());
            writer.WriteLine(IS_INTERNAL_SOUND + "=" + _isInternalSound.ToString());
            writer.WriteLine(SOUND_FILE + "=" + _soundFile.ToString());
            writer.Close();
        }

    }
}
